package japavo.gui.general_ui;

import japavo.gui.components.custom_table.AbstractCustomTableModel;
import japavo.tools.vocabulary.Vocabulary;
import japavo.tools.vocabulary.VocabularyBook;

/**
 *
 */
@SuppressWarnings("serial")
public class EditVocabularyBookTableModel extends AbstractCustomTableModel<String> {

	/**********************************************************************************************
	 ************************************ DECLERATIONS ******************************************** 
	 *********************************************************************************************/
	private VocabularyBook vocabularyBook;
	

	/**********************************************************************************************
	 ************************************ CONSTRUCTORS ******************************************** 
	 *********************************************************************************************/

	/**
	 * 
	 */
	public EditVocabularyBookTableModel(VocabularyBook vocabularyBook) {
		this.vocabularyBook = vocabularyBook;
	}	
	
	/**********************************************************************************************
	 ************************************** FUNCTIONS ********************************************* 
	 *********************************************************************************************/
	private void addRow() {
		vocabularyBook.addVocabulary(new Vocabulary());
	}
	
	public void removeRow(int index) {
		vocabularyBook.removeVocabulary(index);
	}
	
	
	/**********************************************************************************************
	 ********************************* GETTER AND SETTER ****************************************** 
	 *********************************************************************************************/
	
	@Override
	public Object[] getHeaderValues() {
		return new String[] {System.getProperty("NAME_MOTHER_TONGUE"),
							 System.getProperty("NAME_FOREIGN"),
							 System.getProperty("NAME_FOREIGN_LATIN")};
	}

	@Override
	protected String getRowID(int row) {
		Vocabulary vocabulary = vocabularyBook.getVocabulary(row);
		return vocabulary == null ? null : vocabulary.getMotherTongue();
	}

	@Override
	public Object getValueAt(int row, int column) {
		// index too large or last row
		if (row >= vocabularyBook.vocabularyCount())
			return null;
		
		Vocabulary vocabulary = vocabularyBook.getVocabulary(row);
		
		switch (column) {
			case 0:	return vocabulary.getMotherTongue();
			case 1: return vocabulary.getForeign();
			case 2: return vocabulary.getForeignLatin();
			default: return null;
		}
	}
	
	@Override
	public void setValueAt(Object aValue, int row, int column) {
		// Want to set at last row
		if (row == vocabularyBook.vocabularyCount()) {
			// Phantom set
			if (("" + aValue).trim().isEmpty())
				return;
			addRow();
		}
		
		Vocabulary vocabulary = vocabularyBook.getVocabulary(row);
		switch (column) {
			case 0:
				vocabulary.setMotherTongue("" + aValue);
				break;
			case 1:
				vocabulary.setForeign("" + aValue);
				break;
			case 2:
				vocabulary.setForeignLatin("" + aValue);
				break;
		}
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return true;
	}
	
	@Override
	public int getColumnCount() {
		return 3;
	}

	@Override
	public int getRowCount() {
		return vocabularyBook.vocabularyCount() + 1;
	}
}
